﻿/*
Date: 2008/10/23
Author: wangxiang
Email: wxnet2008@gmail.com
version: 2.0.0
*/
(function() {

    //@private 根据pid获取数据
    //@param {string} pid
    //@param {Array} aData 数据
    //@param {HtmlSlect} obj 要装载的下拉列表框
    function getDataBypid(pid, Data, obj) {

        obj = (typeof obj == "string") ? document.getElementById(obj) : obj;

        obj.options.add(new Option("请选择", ""));
        for (var i = 0, len = Data.length; i < len; i++) {
            if (pid != undefined && Data[i][2] == pid) {
                obj.options.add(new Option(Data[i][1], Data[i][0]));
            }
        }
    } //end getDataBypid

    //@private 根据id获取同级
    //@param {string} id id
    //@return {string} 父类id值
    function getpidById(id, data) {
        var pid;
        for (var i = 0, len = data.length; i < len; i++) {
            if (data[i][0] == id) {
                pid = data[i][2];
                break;
            }
        } //end for

        return pid;

    } //getPidById

    //@private 根据value获取SelectIndex
    //@param {HtmlSelect} o  下拉列表框
    //@param {string} value 选项的值
    function getSelectIndexByValue(oSelect, value) {
        for (var i = 0, len = oSelect.options.length; i < len; i++) {

            if (oSelect[i].value == value) {
                return i;
            }
        }
        return "";
    } //end getSelectIndexByValue

    //添加事件    
    function addEvent(obj, type, fn) {
        if (obj.attachEvent) {
            obj['e' + type + fn] = fn;
            obj[type + fn] = function() { obj['e' + type + fn](window.event); };
            obj.attachEvent('on' + type, obj[type + fn]);
        } else
            obj.addEventListener(type, fn, false);
    } // end addEvent


    //@public
    function linkage(aData, aSelects, oHidInput, bDynamic) {

        //获取初始化联动菜单的值
        if (typeof oHidInput == "string") oHidInput = document.getElementById(oHidInput);

        //默认值数组
        var aDefault = (!!oHidInput && oHidInput.value != "") ? oHidInput.value.split(";") : [aData[0][0]],

        i = 0, len = 0;

        //设置为动态显示下级菜单,则隐藏除第一个下拉列表外的其它下拉列表框
        if (!!bDynamic)
            for (i = 1, len = aSelects.length; i < len; i++) document.getElementById(aSelects[i]).style.display = "none";

        //初始化
        for (i = 0, len = aDefault.length; i < len; i++) {

            getDataBypid(getpidById(aDefault[i], aData), aData, aSelects[i]);

            //设置显示状态
            document.getElementById(aSelects[i]).options.selectedIndex = (!!oHidInput && oHidInput.value != "") ? getSelectIndexByValue(document.getElementById(aSelects[i]), aDefault[i]) : 0;
        }

        //给空控件指定默认值
        for (i = 0, len = aSelects.length; i < len; i++) {
            var temp = document.getElementById(aSelects[i]);
            if (temp.options.length == 0)
                temp.options.add(new Option("请选择", ""));
        }

        function fn(v, obj) {
            var up = obj, temp;
            var i = 0, len = 0;

            //装载数据
            for (i = v, len = aSelects.length; i < len; i++) {
                temp = document.getElementById(aSelects[i]);

                //Edit by: Kim Wang Date:2009/2/19
                temp.options.length = 0;

                if (up.value) getDataBypid(up.value, aData, temp);

                //设置下一级的显示
                if (temp.options.length == 0) temp.options.add(new Option("请选择", ""));

                //控制动态显示下一级联动菜单
                if (!!bDynamic) temp.style.display = (temp.options.length == 1) ? "none" : "";

                up = temp;

            }
        }


        //循环绑定事件
        for (i = 0; i < aSelects.length; i++) {
            (function() {
                var iNext = i + 1;
                var o = aSelects[i];
                addEvent(document.getElementById(o), "change", function() { return fn(iNext, this) });
            })();
        }
    }

    window.linkage = linkage;


})();
